<?php function printGroupRow($view,$quantity,$quantity_after_change,$max,$min,$flag_product_code){?>
<tr class='row1'>
	<td colspan='3'></td>		
	<td class='right is_code'>
		<?php echo $view->partial('index/format/float.phtml','admin',array($quantity_after_change)); ?>
	</td>	
	<td colspan='6'></td>
	
	<?php if($view->inventory_lot_code=="" && $view->unit_code!=''){?>
	<td class='right is_code'>
		<?php echo $view->partial('index/format/float.phtml','admin',array($min)); ?>
	</td>
	<td class='right is_code'>
		<?php echo $view->partial('index/format/float.phtml','admin',array($max)); ?>
	</td>
	<td class='is_code'>
		<?php 
		$alert="";
		if($quantity_after_change<$min && !is_string($min))
		{
			$alert="Tồn kho < Tồn kho min ($quantity_after_change < $min)";
		}
		else if($quantity_after_change>$max && !is_string($max))
		{
			$alert="Tồn kho > Tồn kho max ($quantity_after_change > $max)";
		}
		if($alert!="")
		{
		 	$alert="Mã sản phẩm: $flag_product_code<br>".$alert;
		}
		echo $alert;
		?>
	</td>
	<?php }?>
</tr>	
<?php }?>

<script>
$(document).ready(function(){
	$( "#product_group_code" ).change(function(){
		var tmp=$(this);
		$.ajax({
		   type: "POST",
		   url: "<?php echo $this->url(array("controller"=>"inbound-detail","action"=>"ajax-load-product","module"=>"inbound"),null,true)?>",
		   data: {"product_product_group_code":tmp.val(),"product_code":$('#inventory_product_code').val()},
		   success: function(html){
		      $('#inventory_product_code').html(html);
		   }
		 });
	});	
	
	$( "#product_group_code" ).trigger('change');
});
</script>

<!-- thanh toolbar -->
<?php $this->placeholder('toolbar')->captureStart();?>
<?php echo $this->partial('index/toolbar/excel.phtml', 'admin') ?>
<?php echo $this->partial('index/toolbar/excel-all.phtml', 'admin') ?>
<?php $this->placeholder('toolbar')->captureEnd();?>
<!-- / --> 

<form id='adminForm' name='adminForm' method='post' action=''>
<dl class="zend_form">

	<!-- phần tìm kiếm -->	
	<div class='formSearh'>
	<fieldset>
		<legend>Thông tin tìm kiếm</legend>
		<?php echo $this->form->adminAction;?>	
		<?php //echo $this->form->unit_code;?>
		<?php //echo $this->form->warehouse_group_code;?>
		<?php //echo $this->form->product_group_code;?>
		<?php echo $this->form->inventory_product_code;?>		
		<?php //echo $this->form->inventory_lot_code;?>		
	</fieldset>
	</div>	
	<!-- / -->
	
	<!-- danh sách hiển thị -->
	<div class='clr'> </div>
	<table class='adminlist'>
		<thead>			
			<tr>
				<?php echo $this->partial('index/manage/no-save-order.phtml', 'admin'); ?>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Mã hàng hóa','column'=>'inventory_product_code')); ?>
				</th>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Tên hàng hóa','column'=>'product_name')); ?>
				</th>
				<?php if($this->unit_code!=''){?>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'SL sau khi<br/>qui đổi','column'=>'inventory_quantity_after_change')); ?>
				</th>		
				<?php }?>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Số lượng<br/>trong kho','column'=>'inventory_quantity')); ?>
				</th>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Đơn vị tính<br/>trong kho','column'=>'inventory_product_unit_code')); ?>
				</th>	
				<!--<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Vị trí kho','column'=>'location_name')); ?>
				</th>-->
				<!--<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Ngày sản xuất','column'=>'inventory_product_time')); ?>
				</th>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Ngày hết hạn','column'=>'inventory_expire_time')); ?>
				</th>
				<th>
					<?php echo $this->partial('index/manage/header.phtml','admin',array('caption'=>'Mã lot','column'=>'inventory_lot_code')); ?>
				</th>-->
				<?php /*if($this->inventory_lot_code=="" && $this->unit_code!=''){?>
				<th>
					Tồn kho min
				</th>
				<th>
					Tồn kho max
				</th>
				<th>
					Cảnh báo
				</th>
				<?php }*/?>
			</tr>
		</thead>
		<tbody>
		<?php if (count($this->paginator) > 0): $no=1;$flag_product_code="";?>
		<?php foreach ($this->paginator as $row): ?>			
			<?php 
			if($flag_product_code!=$row->inventory_product_code)
			{
				if($flag_product_code!="" && $this->unit_code!="")
					printGroupRow($this,$quantity,$quantity_after_change,$max,$min,$flag_product_code);
				$min=$row->m_Array['product_instore_min'];
				$max=$row->m_Array['product_instore_max'];
				$flag_product_code=$row->inventory_product_code; 
				$quantity=0;
				$quantity_after_change=0;
			}
			$quantity+=$row->inventory_quantity;
			$quantity_after_change+=$row->m_Array['inventory_quantity_after_change'];
			?>			
			<tr class='row0'>
				<?php echo $this->partial('index/manage/order.phtml', 'admin',array($no++)); ?>
				<td>
					<?php echo $row->inventory_product_code; ?>
				</td>
				<td class='left'>
					<?php echo $row->m_Array['product_name']; ?>
				</td>
				<?php if($this->unit_code!=''){?>
				<td class='right'>
					<?php if($row->m_Array['inventory_quantity_after_change']>0 || $row->inventory_quantity==0){?>
						<?php echo $this->partial('index/format/float.phtml','admin',array($row->m_Array['inventory_quantity_after_change'])); ?>
					<?php }else{?>
						<?php echo "-";?>
					<?php }?>
				</td>				
				<?php }?>
				<td class='right'>
					<?php echo $this->partial('index/format/float.phtml','admin',array($row->inventory_quantity)); ?>
				</td>
				<td class='left'>
					<?php echo $row->inventory_product_unit_code; ?>
				</td>
				<!--<td class='left'>
					<?php echo $row->m_Array['location_name']; ?>
				</td>				-->
				<!--<td class='center'>
					<?php echo $this->partial('index/format/time.phtml','admin',array($row->inventory_product_time)); ?>
				</td>
				<td class='center'>
					<?php echo $this->partial('index/format/time.phtml','admin',array($row->inventory_expire_time)); ?>
				</td>
				<td class='left'>
					<?php echo $row->inventory_lot_code; ?>
				</td>-->
				<?php /*if($this->inventory_lot_code=="" && $this->unit_code!=''){?>
				<td></td>
				<td></td>
				<td></td>
				<?php }*/?>
			</tr>
		<?php endforeach;?> 
		<?php if($flag_product_code!="" && $this->unit_code!=""){printGroupRow($this,$quantity,$quantity_after_change,$max,$min,$flag_product_code);}?>		
		<?php endif; ?>
		</tbody>
		<tfoot>
			<tr>
				<td colspan='14'>
					<div class='container'>
						<?php echo $this->paginationControl($this->paginator) ?>
					</div>
				</td>
			</tr>
		</tfoot>
	</table>
	<!-- / -->
	
</dl>
</form>
